package com.wswy.wzcx.ui.main.more

import android.arch.lifecycle.Observer
import android.graphics.Color
import android.os.Bundle
import android.view.ViewGroup
import android.widget.Space
import com.blankj.utilcode.util.SizeUtils
import com.wswy.wzcx.R
import com.wswy.wzcx.model.home.ExtModule
import com.wswy.wzcx.module.base.CBaseFragment
import com.wswy.wzcx.statistics.StatisticsId
import com.wswy.wzcx.widget.home.HomeCategoryLayout
import com.wswy.wzcx.widget.home.ModuleItemClickListener
import kotlinx.android.synthetic.main.fragment_all_modules.*

/**
 * 全部服务
 */
class AllModulesFragment : CBaseFragment() {

    override fun getFragmentTitle(): String {
        return "全部服务"
    }

    override fun getContentLayoutId(): Int {
        return R.layout.fragment_all_modules
    }

    private lateinit var allModulesViewModel: AllModulesViewModel

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        allModulesViewModel = provideViewModel(AllModulesViewModel::class.java)
        allModulesViewModel.onCreate()

        allModulesViewModel.getTabConfLiveData().observe(this, Observer {

            showTabConf(it)
        })

    }



    /**
     * 显示配置
     */
    private fun showTabConf(models:Map<String,List<ExtModule>>?){

        val container=  ll_models_container

        container.removeAllViewsInLayout()

        val clickListener = ModuleItemClickListener(StatisticsId.clickAllModule)


        var idx = 0
        models?.forEach { (t, u) ->

            HomeCategoryLayout(container.context).apply {
                setBackgroundColor(Color.WHITE)
                setLineMaxItem(4)
                showCategory(t,u)
                setOnItemClickListener(clickListener)
                setPadding(0,0,0,SizeUtils.dp2px(8f))
            }.run {
                val  lp = ViewGroup.MarginLayoutParams( ViewGroup.MarginLayoutParams.MATCH_PARENT, ViewGroup.MarginLayoutParams.WRAP_CONTENT)
                //lp.bottomMargin = SizeUtils.dp2px(10f)
                container.addView(this,lp)

                container.addView(Space(context),ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, SizeUtils.dp2px(10f)))

            }
            idx++
        }


    }

}